﻿using iMES.Custom.IServices;
using iMES.Custom.Services;
using iMES.Entity.DomainModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System;
using YUN.Ware.IServices;
using YUN.Ware.Services;
using System.Linq;
using YUN.Ware.IRepositroies;
using YUN.Rawmanagements.IRepositories;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;

namespace iMES.WebApi.Cloudsian
{
    public partial class Ware_ProductController
    {
        private readonly IWare_ProductServices _service;//访问业务代码
        private readonly IBase_RawmanagementRepository _rawMaterialRepository;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public Ware_ProductController(
            IWare_ProductServices service,
            IHttpContextAccessor httpContextAccessor,
            IBase_RawmanagementRepository rawMaterialRepository
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
            _rawMaterialRepository = rawMaterialRepository;
        }

        ////后台App_ProductController中添加代码，返回table数据
        //[HttpPost, Route("getSelectorDemo")]
        //public IActionResult GetSelectorDemo([FromBody] PageDataOptions options)
        //{
        //    //1.可以直接调用框架的GetPageData查询
        //    PageGridData<Ware_Product> data = Ware_ProductServices.Instance.GetPageData(options);

        //    return JsonNormal(data);
        //}
        /// <summary>
        /// 获取选择器示例数据，并包含成品对应的原料编号和原料名称
        /// </summary>
        /// <param name="options">分页数据选项</param>
        /// <returns>包含成品信息及其原料信息的响应</returns>
        [HttpPost, Route("getSelectorDemo")]
        [Authorize] // 如果需要授权
        public IActionResult GetSelectorDemo([FromBody] PageDataOptions options)
        {
            try
            {
                // 1. 直接调用框架的GetPageData查询
                PageGridData<Ware_Product> data = Ware_ProductServices.Instance.GetPageData(options);

                var response = new
                {

                    //通过成品id循环获取原料信息
                    rows = data.rows.Select(product =>
                    {
                        // 获取当前成品对应的原料信息
                        var rawMaterials = _rawMaterialRepository
                            .Find(x => x.RawmanagementId == product.RawmanagementId)
                            .Select(raw => new 
                            {
                                 raw.RawmanagementBh,
                                raw.SizeName,
                                raw.DanWeiName
                            }).FirstOrDefault();
                        // 返回组合后的成品和原料信息
                        return new
                        {
                            product.ProductId,
                            product.ProductCode,
                            product.ProductName,
                            product.ProductType,
                            product.ProductBatch,
                            product.ProductCompany,
                            product.ProductNumS,
                            product.ProductNumX,
                            product.ProductSave,
                            product.ProductState,
                            product.ProductNote,
                            product.ProductImg,
                            product.RawmanagementId,
                            RawmanagementBh = rawMaterials?.RawmanagementBh,
                            SizeName = rawMaterials?.SizeName,
                            DanWeiName = rawMaterials?.DanWeiName
                        };
                    }).ToList(),
                    total = data.total
                };
                 return JsonNormal(response);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return StatusCode(StatusCodes.Status500InternalServerError, "内部服务器错误");
            }
        }
    }
}

// 用于返回成品信息及其原料信息的类
//public class FinishedProductWithRawMaterials
//{
//    public Ware_Product Ware_Product { get; set; }
//    public List<RawMaterialInfo> RawMaterials { get; set; }
//}

// 用于返回原料信息的类
//public class RawMaterialInfo
//{
//    /// <summary>
//    ///原料管理编号
//    /// </summary>
//    [Display(Name = "原料管理名称")]
//    [MaxLength(200)]
//    [Column(TypeName = "nvarchar(200)")]
//    [Editable(true)]
//    public string RawmanagementBh { get; set; }
//    /// <summary>
//    ///尺寸名称
//    /// </summary>

//    [Display(Name = "尺寸名称")]
//    [MaxLength(200)]
//    [Column(TypeName = "nvarchar(200)")]
//    [Editable(true)]
//    public string SizeName { get; set; }

//    /// <summary>
//    ///单位名称
//    /// </summary>

//    [Display(Name = "单位名称")]
//    [MaxLength(200)]
//    [Column(TypeName = "nvarchar(200)")]
//    [Editable(true)]
//    public string DanWeiName { get; set; }
//}

